<template>
    <div class="dateChangeContainer">
        <el-dialog
                title="全局设置"
                ref="date"
                :visible.sync="globalSetVisible"
                width="30%"
                :before-close="closedateChange"
        >
            <div class="box">
                <span class="content">日期</span>
                <el-date-picker
                    v-model="value1"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    @change="changeDate"
                    >
                </el-date-picker>
            </div>
            <div class="box">
                <span class="content">角色</span>
                <el-select :value="roleActive" placeholder="请选择" @change="roleChange">
                    <el-option
                    v-for="item in roleList"
                    :key="item.code"
                    :label="item.name"
                    :value="`${item.guid}#${item.code}`">
                    </el-option>
                </el-select>
            </div>
            <div class="box">
                <span class="content">单位</span>
                <el-select :value="agencyActive" placeholder="请选择" @change="agencyChange">
                    <el-option
                    v-for="item in agencyList"
                    :key="item.CHR_CODE"
                    :label="item.CHR_NAME"
                    :value="item.CHR_CODE">
                    </el-option>
                </el-select>
            </div>
            <div class="box">
                <span class="content">账套</span>
                <el-select :value="accountActive" placeholder="请选择" @change="accountActiveChange">
                    <el-option
                    v-for="item in accountList"
                    :key="item.CHR_CODE"
                    :label="item.CHR_NAME"
                    :value="item.CHR_CODE">
                    </el-option>
                </el-select>
            </div>
            
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="saveDefault" style="float:left;">设为默认并应用</el-button>
                <el-button @click="closedateChange">取 消</el-button>
                <el-button type="primary" @click="saveGlobalData">应用</el-button>
            </span>
        </el-dialog>
        
    </div>
</template>

<script>
let Base64 = require('js-base64').Base64;
import appUtil from '~/assets/utils/appUtil.js';
import { createNamespacedHelpers } from 'vuex';
const { mapState, mapMutations, mapActions } = createNamespacedHelpers('homeModule');
export default {
    name: 'globalSet',
    props: ['globalSetVisible'],
    data () {
        return {

            value1: '',
            roles:[]
        };
    },
    computed:{
        ...mapState({
            roleList:state => state.roleList,
            roleActive:state => state.roleActive,
            agencyList:state => state.agencyList,
            agencyActive:state => state.agencyActive,
            accountList:state => state.accountList,
            accountActive:state => state.accountActive
        }),
    },
    created(){
        let dateV = Base64.decode(JSON.parse(localStorage.getItem('commonData')).svTransDate);
        this.value1 = dateV;
        this.getRole();
        this.getAgency();
        this.getAccount();
    },
    methods: {
        ...mapMutations(['roleActiveChange','agencyActiveChange','accountActiveChange']),
        ...mapActions(['getRoleListAsync','getAgencyListAsync','getAccountListAsync','saveGlobalAsync','saveDefaultSetAsync']),
        
        // 关闭弹框
        closedateChange () {
            this.$parent.closeDate();
        },
        // 获取角色
        getRole(){
            let data = {
                ruleID:'portal-baseData.getRoleDataByUserId',
                userId:appUtil.getSes('userId'),
                start:0,
                limit:50,
                setYear:Base64.decode(JSON.parse(localStorage.getItem('commonData')).svSetYear),
                rgCode:Base64.decode(JSON.parse(localStorage.getItem('commonData')).svRgCode)
            };
            this.getRoleListAsync(data);
        },
        // 获取单位
        getAgency(){
            let agency = {
                userId:appUtil.getSes('userId'),
                roleId:this.roleActive===''?Base64.decode(JSON.parse(localStorage.getItem('commonData')).svRoleId):this.roleActive.split('#')[0],
                nd:this.value1.slice(0,4),//Base64.decode(JSON.parse(localStorage.getItem('commonData')).svSetYear),
                rgCode:Base64.decode(JSON.parse(localStorage.getItem('commonData')).svRgCode)
            };
            this.getAgencyListAsync(agency);
            
        },
        // 获取账套
        getAccount(){
            let account = {
                userId:appUtil.getSes('userId'),
                roleId:this.roleActive===''?Base64.decode(JSON.parse(localStorage.getItem('commonData')).svRoleId):this.roleActive.split('#')[0],
                nd:this.value1.slice(0,4),//Base64.decode(JSON.parse(localStorage.getItem('commonData')).svSetYear),
                rgCode:Base64.decode(JSON.parse(localStorage.getItem('commonData')).svRgCode),
                agencyCode:this.agencyActive===''?Base64.decode(JSON.parse(localStorage.getItem('commonData')).svAgencyCode):this.agencyActive
            };
            this.getAccountListAsync(account);
        },
        //  切换日期
        changeDate(){
            // this.getRole();
            this.getAgency();
            this.getAccount();
        },
        // 切换角色
        roleChange(code){
            this.roleActiveChange(code);
            this.getAgency();
            this.getAccount();
        },
        // 切换单位
        agencyChange(code){
            this.agencyActiveChange(code);
            this.getAccount();
        },
        // 应用和默认应该公共参数
        paramCommon(){
            let role = this.roleActive;
            let roleName = this.roleList.filter(function(item){
                return role === item.guid+'#'+item.code;
            });

            let agency = this.agencyActive;
            let agencyName = this.agencyList.filter(function(item){
                return agency === item.CHR_CODE;
            });

            let account = this.accountActive;
            let accountName = this.accountList.filter(function(item){
                return account === item.CHR_CODE;
            });
            
            let param = {
                userId:appUtil.getSes('userId'),
                svAgencyCode:this.agencyActive,
                svAgencyName:agencyName[0].CHR_NAME,
                svAccountCode:this.accountActive,
                svAccountName:accountName[0].CHR_NAME,
                svRoleId:this.roleActive.split('#')[0],
                svRoleCode:this.roleActive.split('#')[1],
                svRoleName:roleName[0].name,
                setYear:this.value1.slice(0,4),
                svTransDate:this.value1
            };
            return param;
        },
        // 应用
        saveGlobalData(){
            // 子组件调用父组件的方法
            this.$parent.closeDate();
            this.saveGlobalAsync(this.paramCommon()).then(res => {
                if(res.data){
                    this.$message.success('保存成功');
                    window.location.reload();
                }
            });
        },
        // 设为默认并应用
        saveDefault(){
            // 子组件调用父组件的方法
            this.$parent.closeDate();
            this.saveDefaultSetAsync(this.paramCommon()).then(res => {
                if(res.data){
                    this.$message.success('保存成功');
                    window.location.reload();
                }
            });
        }
    },
};
</script>

<style lang="scss" scoped>
.dateChangeContainer{
    text-align: center;
    margin: auto;
     .el-dialog__header{
        text-align: left;
    }
    .box{
        margin-top: 10px;
        .content{
            margin-right: 10px;
        }
    }
}

</style>
